<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/19 0019
 * Time: 下午 6:53
 * Administrator
 */

namespace app\common\util;


use app\common\model\SysLogPush;

require_once(dirname(__FILE__) . '/' . 'IGeTui.php');
require_once(dirname(__FILE__) . '/' . 'igetui/IGt.AppMessage.php');
require_once(dirname(__FILE__) . '/' . 'igetui/IGt.APNPayload.php');
require_once(dirname(__FILE__) . '/' . 'igetui/template/IGt.BaseTemplate.php');
require_once(dirname(__FILE__) . '/' . 'IGt.Batch.php');
require_once(dirname(__FILE__) . '/' . 'igetui/utils/AppConditions.php');

trait GetuiUtil
{
    protected static $host = 'http://sdk.open.api.igexin.com/apiex.htm';
    protected static $appid = 'Cych1heOd39fdmnRjzhBs2';
    protected static $appkey = 'Ee8YggWOVZ6avbzx5UwAE6';
    protected static $mastersecret = 'ncSz12WoiU95PXiz7MVBF4';

    /*  单推功能  用于一次创建提交多个单推任务 */
    public function pushMessageToSingle($CID,$messTitle,$messSummary,$payload)
    {
        $slp = SysLogPush::create([
            'cid'=>$CID
            ,'messTitle'=>$messTitle
            ,'messSummary'=>$messSummary
            ,'payload'=>$payload
            ,'type'=>10
        ]);
        trace($slp,'pushMessageToSingle');
        $igt = new IGeTui(self::$host,self::$appkey,self::$mastersecret);
        $batch = new IGtBatch(self::$appkey, $igt);
        $batch->setApiUrl(self::$host);
        $messTitle   = $messTitle ? $messTitle : 'vovoqi';
        $templateLink = $this->IGtTransmissionTemplateDemo($messTitle,$messSummary,$payload);
        //个推信息体
        $messageLink = new \IGtSingleMessage();
        $messageLink->set_isOffline(true);//是否离线
        $messageLink->set_offlineExpireTime(12 * 1000 * 3600);//离线时间
        $messageLink->set_data($templateLink);//设置推送消息类型
        $messageLink->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送

        $targetLink = new \IGtTarget();
        $targetLink->set_appId(self::$appid);
        $targetLink->set_clientId($CID);
        $batch->add($messageLink,$targetLink);
        try {
            $rep = $batch->submit();
            trace($rep,'getui.result');
            if ($rep['result'] == 'ok'){
                $slp->save([
                    'taskId'=>$rep['info']['taskId']
                    ,'status'=>$rep['info']['status']
                ]);
            }
            return $rep;
        }catch(\Exception $e){
            $rep=$batch->retry();
            trace($rep,'getui.result');
            return $rep;
        }
    }

    public function pushMessageToApp($messId,$messType,$messTitle,$messSummary){
        $igt = new IGeTui(self::$host,self::$appkey,self::$mastersecret);
        $messTitle   = $messTitle ? $messTitle :'神利金';
        $payload='{"messId":"'.$messId.'","messType":"'.$messType.'","$messSummary":"'.$messSummary.'"}';

        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = $this->IGtTransmissionTemplateDemo($messTitle,$messSummary,$payload);
        //$template = IGtLinkTemplateDemo();
        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        $message = new \IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);
        $appIdList=array(self::$appid);
        $message->set_appIdList($appIdList);
        $rep = $igt->pushMessageToApp($message,"任务组名");
        return $rep;
    }

    //透传消息模版
    public  function IGtTransmissionTemplateDemo($title,$content,$payload){
        $template =  new \IGtTransmissionTemplate();
        //应用appid
        $template->set_appId(self::$appid);
        //应用appkey
        $template->set_appkey(self::$appkey);
        //透传消息类型
        $template->set_transmissionType(2);
        //透传内容
        //安卓推送数据设置
        $template->set_transmissionContent('{"title":"'.$title.'","content":"'.$content.'","payload":'.$payload.'}');
//          $template->set_transmissionContent('{"payload":'.$payload.'}');
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息

        // 如下有两个推送模版，一个简单一个高级，可以互相切换使用。此处以高级为例，所以把简单模版注释掉。
        //        APN简单推送
        //      $apn = new IGtAPNPayload();
        //      $alertmsg=new SimpleAlertMsg();
        //      $alertmsg->alertMsg="";
        //      $apn->alertMsg=$alertmsg;
        //      $apn->badge=2;
        //      $apn->sound="";
        //      $apn->add_customMsg("payload","payload");
        //      $apn->contentAvailable=1;
        //      $apn->category="ACTIONABLE";
        //      $template->set_apnInfo($apn);

        //       APN高级推送 ios版本

        //苹果推送数据设置
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body=$content;
        $alertmsg->actionLocKey="ActionLockey";
        /*   $alertmsg->locKey="LocKey"; */
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
        //        iOS8.2 支持
        $alertmsg->title=$title;
        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        $apn->badge=1;
        $apn->sound="";
        $apn->add_customMsg("payload",$payload);
        //      $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        return $template;
    }
}